package com.shangxian.giveithome.service.impl;

import com.shangxian.giveithome.code.CommentCode;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.Comment;
import com.shangxian.giveithome.entity.community.CommetList;
import com.shangxian.giveithome.entity.community.NoticeCenter;
import com.shangxian.giveithome.entity.community.UserList;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.AttentionMapper;
import com.shangxian.giveithome.mapper.CommentMapper;
import com.shangxian.giveithome.mapper.IssueMapper;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.CommentService;
import com.shangxian.giveithome.utils.PageUtils;
import com.shangxian.giveithome.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private IssueMapper issueMapper;

    @Resource
    private AttentionMapper attentionMapper;


    @Override
    public PageableDetails findCommentList(Long id, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = commentMapper.findCommentNumber(id);
        if (total == 0){
            return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList());
        }
        List<CommetList> commentList = commentMapper.findCommentListById(id,pageNumber*pageSize,pageSize);
        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),commentList);
    }

    @Override
    public void addComment(Comment comment, HttpServletRequest request) {
        UserDetails userDetails = redisUtils.getHeader(request);
        comment.setCuId(userDetails.getId());
        comment.setCommentTime(new Date());
        long usId = issueMapper.findUsIdByIssueId(comment.getIssueId());
        comment.setUsId(usId);
        comment.setState(0L);
        if (comment.getIssueId() ==null||comment.getIssueId() ==0){
             ExceptionUtils.error(CommentCode.INSERT_ERROR);
        }
        int i = commentMapper.addComment(comment);
        if (i<1){
            Result.error(CommentCode.UPLOAD_ERROR);
        }
    }

    @Override
    public void deleteCommentById(Long id) {
        int i = commentMapper.deleteCommentById(id);
        if (i<1){
            Result.error(CommentCode.DELETE_ERROR);
        }
    }

    @Override
    public void readComment(Long id) {
        int i = commentMapper.readComment(id);
        if (i<1){
            Result.error(CommentCode.UPLOAD_ERROR);
        }
    }

    @Override
    public void readAllComment(HttpServletRequest request) {
        UserDetails userDetails = redisUtils.getHeader(request);
        int i = commentMapper.readAllComment(userDetails.getId());
        if (i<1){
            Result.error(CommentCode.UPLOAD_ERROR);
        }
    }

    @Override
    public NoticeCenter findNewestComment(HttpServletRequest request) {
        UserDetails userDetails = redisUtils.getHeader(request);
        NoticeCenter newestComment = commentMapper.findNewestComment(userDetails.getId());
        NoticeCenter newestComents = redisUtils.getRedis("newestComent");
        if (newestComment == null){
            NoticeCenter noticeCenter = new NoticeCenter();
            noticeCenter.setState(1L);
            return noticeCenter;
        }else {
        if (newestComents.getId()!= null && newestComents.getId().equals(newestComment.getId())) {
            NoticeCenter noticeCenter = new NoticeCenter();
            noticeCenter.setState(1L);
            return noticeCenter;
        }
            UserList userList = attentionMapper.findUserList(newestComment.getCuId());
            newestComment.setName(userList.getName());
            newestComment.setPath(userList.getPicture());
            redisUtils.setKey("newestComent", newestComment);
            return newestComment;
        }
    }


    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }

    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }


}